<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
defined('_VALID_MOS') or die('Direct Access to this location is not allowed.');

switch ($task) {

    case 'open':
        LM::setProject($cid[0]);
        LM::setTeam(null);
        LM::redirect();
        break;


    case "rounds":
    case "matrix":
    case "matches":
    case "teammap":
    case "leaguetable":

        $row = LM::getLMTable("Project");
        if (!$row->load($cid[0])) {
            LM::redirect("projects", "No such Project available");
        }

        LM::setProject($row->getID());
        LM::setTeam(null);
        LM::setSessionValue("_filterresultsbyround_", $row->getCurrentRound());
        LM::redirect($task);
        break;

    case 'close':
        LM::setProject(null);
        LM::setTeam(null);
        LM::redirect();
        break;

    case 'new':
    case 'edit':

        if (isAdmin ()) {

            $row = LM::getLMTable("Project");

            if ($task == 'edit') {
                $id = intval(mosGetParam($_REQUEST, 'id', $cid[0]));
                if (!$row->load($id)) {
                    LM::redirect("projects", "No such Project available!");
                    exit();
                }
            }

            $eventtypetable = LM::getLMTable("Eventtype");
            $eventtypes = $eventtypetable->getRows();

            $template = LM::getTemplate();

            $template->assign("view", "admin/form_projects.tpl.php");
            $template->assign("project", $project);
            $template->assign("team", $team);
            $template->assign("my", $my);
            $template->assign("eventtypes", $eventtypes);
            $template->assign("row", $row);

            $template->display("admin/view.tpl.php");
        }
        break;

    case 'copy':

        if (isAdmin ()) {

            $row = LM::getLMTable("Project");
            if (!$row->load(intval($cid[0]))) {
                LM::redirect("projects", "No such Project available");
            }

            $copy = $row;
            $copy->id = null;
            $copy->project_name = "Copy of " . $row->getName();
            $copy->published = 0;
            $copy->finished = 0;
            $copy->store();

            $sql = "SELECT * FROM #__leagueman_round WHERE round_project = " . $row->getID();
            $database->setQuery($sql);
            $rounds = $database->loadObjectList();

            $i = 0;
            foreach ($rounds as $round) {
                $new_round = LM::getLMTable("Round");
                $new_round->round_name = $round->round_name;
                $new_round->round_name_short = $round->round_name_short;
                $new_round->ordering = $round->ordering;
                $new_round->round_project = $copy->getID();
                if ($new_round->store()) {
                    $i++;
                }
            }

            $sql = "SELECT * FROM #__leagueman_teammap WHERE teammap_project = " . $row->getID();
            $database->setQuery($sql);
            $teammaps = $database->loadObjectList();

            $j = 0;
            foreach ($teammaps as $teammap) {
                $new_teammap = LM::getLMTable("Teammap");
                $new_teammap->teammap_project = $copy->getID();
                $new_teammap->teammap_team = $teammap->teammap_team;
                $new_teammap->teammap_points = 0;
                $new_teammap->teammap_user = $teammap->teammap_user;
                if ($new_teammap->store()) {
                    $j++;
                }
            }

            $sql = "SELECT * FROM #__leagueman_playermap WHERE playermap_project = " . $row->getID();
            $database->setQuery($sql);
            $playermaps = $database->loadObjectList();

            $k = 0;
            foreach ($playermaps as $playermap) {
                $new_playermap = LM::getLMTable("Playermap");
                $new_playermap->playermap_player = $playermap->playermap_player;
                $new_playermap->playermap_team = $playermap->playermap_team;
                $new_playermap->playermap_project = $copy->getID();
                $new_playermap->position_number = $playermap->position_number;
                $new_playermap->playermap_position = $playermap->playermap_position;
                if ($new_playermap->store()) {
                    $k++;
                }
            }

            $template = LM::getTemplate();

            $template->assign("view", "admin/copy_projects.tpl.php");
            $template->assign("project", $project);
            $template->assign("team", $team);
            $template->assign("copy", $copy);
            $template->assign("i", $i);
            $template->assign("j", $j);
            $template->assign("k", $k);
            $template->display("admin/view.tpl.php");
        }
        break;

    case 'save':

        if (isAdmin ()) {

            $row = LM::getLMTable("Project");

            $row->bind($_POST);
            $row->eventtypes = serialize($_POST["eventtypes"]);
            $row->params = serialize($_POST["params"]);
            $row->store();
        }

        LM::redirect();
        break;

    case 'cancel':
        LM::redirect();
        break;

    case 'publish':
        $row = LM::getLMTable("Project");
        $row->publish($cid, 1, $my->id);
        LM::redirect();
        break;

    case 'unpublish';
        $row = LM::getLMTable("Project");
        $row->publish($cid, 0, $my->id);
        LM::redirect();
        break;

    case 'finish':
        $project_id = intval($cid[0]);
        $database->setQuery("UPDATE #__leagueman_project SET finished = 1 WHERE project_id = {$project_id} LIMIT 1");
        $database->query();
        LM::redirect();
        break;

    case 'unfinish':
        $project_id = intval($cid[0]);
        $database->setQuery("UPDATE #__leagueman_project SET finished = 0 WHERE project_id = {$project_id} LIMIT 1");
        $database->query();
        LM::redirect();
        break;

    case 'delete':

        if (isAdmin ()) {

            $project_id = intval(mosGetParam($_POST, 'project_id', 0));

            if ($project_id > 0) {
                $row = LM::getLMTable("Project");
                $row->load($project_id);

                $msg = array();

                $database->setQuery("DELETE FROM #__leagueman_match WHERE match_project = " . $row->getID());
                if ($database->query()) {
                    $msg[] = $database->getAffectedRows() . " Matches deleted";
                }

                $database->setQuery("DELETE FROM #__leagueman_round WHERE round_project = " . $row->getID());
                if ($database->query()) {
                    $msg[] = $database->getAffectedRows() . " Rounds deleted";
                }

                $database->setQuery("DELETE FROM #__leagueman_teammap WHERE teammap_project = " . $row->getID());
                if ($database->query()) {
                    $msg[] = $database->getAffectedRows() . " Teams deleted";
                }

                $database->setQuery("DELETE FROM #__leagueman_playermap WHERE playermap_project = " . $row->getID());
                if ($database->query()) {
                    $msg[] = $database->getAffectedRows() . " Player deleted";
                }

                if ($row->delete()) {
                    $msg[] = "Project deleted";
                }

                LM::redirect("projects", implode(", ", $msg));
            }
        }

        LM::redirect();
        break;

    case 'deleteconfirm':

        if (isAdmin ()) {

            $row = LM::getLMTable("Project");
            $row->load(intval($cid[0]));

            if ($project instanceof LM_Table_Project && $row->getID() == $project->getID()) {
                LM::redirect("projects", "Current Project can not be deleted!");
            }

            $database->setQuery("SELECT COUNT(round_id) FROM #__leagueman_round WHERE round_project = " . $row->getID());
            $c["rounds"] = $database->loadResult();

            $database->setQuery("SELECT COUNT(match_id) FROM #__leagueman_match WHERE match_project = " . $row->getID());
            $c["matches"] = $database->loadResult();

            $database->setQuery("SELECT COUNT(teammap_id) FROM #__leagueman_teammap WHERE teammap_project = " . $row->getID());
            $c["teams"] = $database->loadResult();

            $database->setQuery("SELECT COUNT(playermap_id) FROM #__leagueman_playermap WHERE playermap_project = " . $row->getID());
            $c["players"] = $database->loadResult();

            $template = LM::getTemplate();

            $template->assign("view", "admin/confirm_delete_projects.tpl.php");
            $template->assign("project", $project);
            $template->assign("team", $team);
            $template->assign("c", $c);
            $template->assign("row", $row);
            $template->display("admin/view.tpl.php");
        }

        break;

    case "createforum":

        $id = intval(mosGetParam($_POST, "project_id", 0));

        if (!$id) {
            LM::redirect("projects", "Please post a project ID!");
        }

        $row = LM::getLMTable("Project");
        if (!$row->load($id)) {
            LM::redirect("projects", "Could not load project");
        }

        if ($row->getParam("forum", 0) != 0) {
            LM::redirect("projects", "There is already a Forum for this project!");
        }

        $forumid = $row->createForum();

        if (!$forumid) {
            LM::redirect("projects", "Could not create Forums");
        }

        $params = $row->getParams();
        $params["forum"] = $forumid;

        $row->params = serialize($params);
        if (!$row->store()) {
            LM::redirect("projects", "Could not store Params");
        }

        mosRedirect("index2.php?option=com_leagueman&act=projects&task=edit&cid[]=" . $row->getID());
        break;

    default:

        $limit = intval(LM::getSessionValueFromRequest("_{$act}_limit", "limit", 10));
        $limitstart = intval(LM::getSessionValueFromRequest("_{$act}_limitstart", "limitstart", 0));

        $defaults["league_id"] = intval(LM::getSessionValueFromRequest("_filterbyleague_", "league", 0));
        $defaults["season_id"] = intval(LM::getSessionValueFromRequest("_filterbyseason_", "season", 0));
        $defaults["template"] = LM::getSessionValueFromRequest("_filterbytemplate_", "template", "");
        $defaults["finished"] = intval(LM::getSessionValueFromRequest("_filterbyfinished_", "finished", 0));

        $projecttable = LM::getLMTable("Project");
        $projecttable->addFilter("a.finished = " . $database->Quote($defaults["finished"]));

        if ($defaults["league_id"] > 0)
            $projecttable->addFilter("l.league_id = " . $database->Quote($defaults["league_id"]));
        if ($defaults["season_id"] > 0)
            $projecttable->addFilter("s.season_id = " . $database->Quote($defaults["season_id"]));
        if ($defaults["template"] != "")
            $projecttable->addFilter("a.project_template = " . $database->Quote($defaults["template"]));

        if (!isAdmin()) {
            $projecttable->addFilter("a.project_admin = " . $database->Quote(intval($my->id)));
        }

        $pageNav = LM::getPageNav($projecttable->getTotalRows(), $limitstart, $limit, 1);

        $rows = $projecttable->getRows($limitstart, $limit);

        $template = LM::getTemplate();

        $template->assign("view", "admin/list_projects.tpl.php");
        $template->assign("project", $project);
        $template->assign("team", $team);
        $template->assign("defaults", $defaults);
        $template->assign("rows", $rows);
        $template->assign("my", $my);
        $template->assign("pageNav", $pageNav);

        $template->display("admin/view.tpl.php");
        break;
}

